package com.zhuang.taiqiu.netty.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuang.taiqiu.config.GCmd;
import com.zhuang.taiqiu.config.SessionManage;
import com.zhuang.taiqiu.model.Coordinate;
import com.zhuang.taiqiu.model.CueData;
import com.zhuang.taiqiu.model.FrameInfo;
import com.zhuang.taiqiu.model.RoomInfo;
import com.zhuang.taiqiu.model.vo.RoomInfoVO;
import com.zhuang.taiqiu.proto.GameMessageOuterClass;
import com.zhuang.taiqiu.utils.NettyUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CueHandler {



    public void moveSync(ChannelHandlerContext ctx, GameMessageOuterClass.GameMessage msg) {

        JSONObject jsonParse = JSON.parseObject(msg.getData());
        Long roomId = jsonParse.getLong("roomId");

        FrameInfo frameInfo = new FrameInfo();
        RoomInfoVO roomInfoVO = new RoomInfoVO();
        roomInfoVO.setRoomId(roomId);
        /**获取房间对象*/
        RoomInfo roomInfo = SessionManage.getChannelRoomInfo(roomId);
        if(null == roomInfo){
            NettyUtil.sendMsg(ctx.channel(), frameInfo, GCmd.S2C.REQUEST_CUE_FAIL.getDescription());
            return;
        }

        /**保存当前帧，是谁发的，谁打的球**/
        /**既然是状态同步**/

        Long userId = jsonParse.getLong("userId");
        JSONObject directionJson = jsonParse.getJSONObject("direction");
        JSONObject powerJson = jsonParse.getJSONObject("power");

        /**做好用户击球状态才做判断*/
//        if(null != roomInfo.getCurrentPlayer() && !roomInfo.getCurrentPlayer().equals(userId)){
//            /**对不起，当前不是发球用户*/
//            NettyUtil.sendMsg(ctx.channel(), frameInfo, GCmd.S2C.REQUEST_CUE_FAIL.getDescription());
//            return;
//        }

        frameInfo.setCurrentPlayer(userId);
        CueData cueData = new CueData();
        /**判断是否球杆力度*/
        if(null != directionJson){
            Coordinate dc = new Coordinate();
            dc.setX(directionJson.getDouble("x"));
            dc.setY(directionJson.getDouble("y"));
            dc.setZ(directionJson.getDouble("z"));
            dc.setW(directionJson.getDouble("w"));
            cueData.setDirection(dc);
        }
        /**判断是否球杆角度*/
        if(null != powerJson){
            Coordinate dc = new Coordinate();
            dc.setX(powerJson.getDouble("x"));
            dc.setY(powerJson.getDouble("y"));
            dc.setZ(powerJson.getDouble("z"));
            dc.setW(powerJson.getDouble("w"));
            cueData.setPower(dc);
        }
        frameInfo.setCueData(cueData);
        /**这个动作，需要保存在数据库里，保存在内存了，下次断线重连同步的时候，带上，这个就是操作帧，状态
         *
         * 这里应该群发到同房间的账号上
         * */

        /**当前帧记录房间信息【这个位置评估下，在当前帧是不是不用记录房间信息】*/
//        frameInfo.setRoomInfo(roomInfoVO);
        /**记录当前帧*/
        List<FrameInfo> frameInfoList = roomInfo.getFrameInfoList();
        frameInfoList.add(frameInfo);

        ChannelGroup channelGroup = roomInfo.getChannelGroup();
        NettyUtil.sendGroupMsg(channelGroup, frameInfo, GCmd.S2C.REQUEST_CUE_SYNC.getDescription());


    }
}
